﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqSpouse
    {
        public List<Spouse> GetSpouses(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<SpouseEntity> query = db.SpouseEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "SpouseID":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.SpouseID);
                            else
                                query = query.OrderByDescending(c => c.SpouseID);
                            break;
                        case "CustomerID":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.CustomerID);
                            else
                                query = query.OrderByDescending(c => c.CustomerID);
                            break;
                        case "Initial":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.Initial);
                            else
                                query = query.OrderByDescending(c => c.Initial);
                            break;
                        //case "FirstName":
                        //    if (sortOrder == "ASC")
                        //        query = query.OrderBy(c => c.FirstName);
                        //    else
                        //        query = query.OrderByDescending(c => c.FirstName);
                        //    break;
                        //case "LastName":
                        //    if (sortOrder == "ASC")
                        //        query = query.OrderBy(c => c.LastName);
                        //    else
                        //        query = query.OrderByDescending(c => c.LastName);
                        //    break;
                        //case "IDNumber":
                        //    if (sortOrder == "ASC")
                        //        query = query.OrderBy(c => c.IDNumber);
                        //    else
                        //        query = query.OrderByDescending(c => c.IDNumber);
                        //    break;

                    }
                }
                return query.Select(c => Mapper.ToSpouseObject(c)).ToList();
            }
        }


        public Spouse GetSpouse(int spouseId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToSpouseObject(db.SpouseEntities
                            .SingleOrDefault(p => p.SpouseID == spouseId));
            }
        }


        public Spouse GetSpouseForCustomer(string customerId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    return Mapper.ToSpouseObject(db.SpouseEntities
                            .SingleOrDefault(p => p.CustomerID == customerId));
                }
                catch (Exception)
                {

                    throw new Exception("Could not found spouse details");
                }
                
            }
        }



        public void InsertSpouse(Spouse spouse)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    SpouseEntity entity = Mapper.ToSpouseEntity(spouse);
                    db.SpouseEntities.InsertOnSubmit(entity);
                    db.SubmitChanges();

                    spouse.SpouseId = entity.SpouseID;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    throw new Exception("Unable to insert spouse " + spouse.SpouseId);
                }
            }
        }


        public int UpdateCustomer(Spouse spouse)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    SpouseEntity entity = Mapper.ToSpouseEntity(spouse);
                    db.SpouseEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to spouse record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }


        public int DeleteCustomer(Spouse spouse)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    SpouseEntity entity = Mapper.ToSpouseEntity(spouse);
                    db.SpouseEntities.Attach(entity, false);
                    db.SpouseEntities.DeleteOnSubmit(entity);
                    db.SubmitChanges();

                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to spouse record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }
    }
}
